package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import conexion.BDConexion;
import beans.Perfil;
import dao.interfaces.PerfilDAO;

public class MySqlPerfilDAO implements PerfilDAO {

	@Override
	public List<Perfil> ListarPerfil() {
		List<Perfil> lista = new ArrayList<Perfil>();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "SELECT * FROM bd_inictel_dcc.perfil;";
			pst = cn.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				Perfil d = new Perfil(rs.getInt(1), rs.getString(2));
				lista.add(d);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return lista;
	}

	@Override
	public List<Perfil> ListarPerfilxUsu(String codusu) {
		List<Perfil> lista = new ArrayList<Perfil>();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "CALL bd_inictel_dcc.ListaPerfilxUsu(?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, codusu);
			rs = pst.executeQuery();
			while (rs.next()) {
				Perfil d = new Perfil(rs.getInt(1), rs.getString(2));
				lista.add(d);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return lista;
	}

	@Override
	public int AgregarPerfil(String codusu, int codper) {
		int salida =-1;
		Connection cn = null;
		PreparedStatement pst = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "CALL bd_inictel_dcc.InsertPerfil(?,?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, codusu);
			pst.setInt(2, codper);
			
			salida = pst.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return salida;
	}

	@Override
	public int EliminarPerfil(String codusu, int codper) {
		int salida =-1;
		Connection cn = null;
		PreparedStatement pst = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "CALL bd_inictel_dcc.DeletePerfil(?,?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, codusu);
			pst.setInt(2, codper);
			
			salida = pst.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return salida;
	}

	@Override
	public Perfil BuscaPerfil(String codusu, int codper) {
		Perfil per = new Perfil();
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "CALL bd_inictel_dcc.SearchPerfil(?,?);";
			pst = cn.prepareStatement(sql);
			pst.setString(1, codusu);
			pst.setInt(2, codper);
			rs = pst.executeQuery();
			while (rs.next()) {
				per.setDescripcion(rs.getString(1));
				per.setCodigo(rs.getInt(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				pst.close();
				rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return per;
	}

	

}
